﻿/*
=======================================================
    User【用户】
=======================================================
*/

namespace Services
{
    public interface IUserMapping : IMapping<User, UserModel, UserFilterModel>
    { }

    public class UserMapping : AutoUserMapping, IUserMapping
    {
        private readonly ILoginUserDataPermission _ILoginUserDataPermission;

        public UserMapping(ICurrentUser currentUser, ILoginUserDataPermission iLoginUserDataPermission)
            : base(currentUser)
        {
            _ILoginUserDataPermission = iLoginUserDataPermission;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override UserModel Entity2Model(User entity)
        {
            if (entity == null)
            {
                return null;
            }

            UserModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/

            model.RoleIDs = entity.RoleIDs;
            model.DepartmentUserID = entity.DepartmentUserID;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override User Model2Entity(UserModel model)
        {
            User entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/

            entity.RoleIDs = model.RoleIDs;

            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<UserModel> Entity2Model(IEnumerable<User> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<User> Model2Entity(IEnumerable<UserModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<User> FilterModel2Where(UserFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                if (_currentUser.CurrentUserInfo.LoginUserRoleDepartment.Item1 == false)
                {
                    var pkIDs = _ILoginUserDataPermission.GetTablePkIDs(DataDepartment_Type.用户, null, _currentUser.CurrentUserInfo.LoginUserRoleDepartment.Item2.ToArray());
                    where = where.And(x => pkIDs.Contains(x.UserID));
                }
            }


            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            ////登录密码
            //if (!string.IsNullOrEmpty(filterModel._LoginPassword))
            //{
            //    where = where.And(x => x.LoginPassword.Contains(filterModel._LoginPassword));
            //    //where = where.And(x => x.LoginPassword == filterModel._LoginPassword);
            //}

            ////邮箱
            //if (!string.IsNullOrEmpty(filterModel._Email))
            //{
            //    where = where.And(x => x.Email.Contains(filterModel._Email));
            //    //where = where.And(x => x.Email == filterModel._Email);
            //}

            ////手机号
            //if (!string.IsNullOrEmpty(filterModel._Phone))
            //{
            //    where = where.And(x => x.Phone.Contains(filterModel._Phone));
            //    //where = where.And(x => x.Phone == filterModel._Phone);
            //}

            ////身份证号
            //if (!string.IsNullOrEmpty(filterModel._IdCard))
            //{
            //    where = where.And(x => x.IdCard.Contains(filterModel._IdCard));
            //    //where = where.And(x => x.IdCard == filterModel._IdCard);
            //}

            ////创建人
            //if (filterModel._CreateUserID.HasValue)
            //{
            //    where = where.And(x => x.CreateUserID == filterModel._CreateUserID.Value);
            //}

            ////修改人
            //if (filterModel._UpdateUserID.HasValue)
            //{
            //    where = where.And(x => x.UpdateUserID == filterModel._UpdateUserID.Value);
            //}


            return where;
        }
        #endregion
    }
}
